using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;


namespace QQZiFramework
{
    /// <summary>
    /// 数据库字段的用途。
    /// </summary>
    public enum DBFieldUsage
    {
        /// <summary>
        /// 未定义。
        /// </summary>
        None = 0x00,
        /// <summary>
        /// 用于主键。
        /// </summary>
        PrimaryKey = 0x01,
        /// <summary>
        /// 用于唯一键。
        /// </summary>
        ForeignKey = 0x02,
        /// <summary>
        /// 由系统控制该字段的值。
        /// </summary>
        MarkDelete = 0x04,
        /// <summary>
        /// 不是本张表的
        /// </summary>
        NoField = 0X08
    }


    [AttributeUsage(AttributeTargets.Property, Inherited = true)]
    public class DBFieldAttribute : Attribute
    {
        public string FieldName { get; set; }

        public DBFieldUsage Usage { get; set; }

        int[] markDeleteValue;

        static int[] Default_DBFieldMarkDeleteValue = new int[] { 1, -1 };


        public int[] MarkDeleteValue
        {
            get
            {
                if (markDeleteValue == null) return Default_DBFieldMarkDeleteValue;
                else
                {
                    if (markDeleteValue.Length != 2) throw new Exception("MarkDeleteValue长度为2,默认值,删除值");
                    return markDeleteValue;
                }
            }
            set
            {
                markDeleteValue = value;
            }
        }

        public string ForeignTable { get; set; }

        /// <summary>
        /// 外键对应的实体属性
        /// </summary>
        public string ForeignProperty { get; set; }

        /// <summary>
        /// 是否数据库自增
        /// </summary>
        public bool Identity { get; set; }

        /// <summary>
        /// 指定的OEM执行*号查询时不包括该列
        /// </summary>
        public string ExcludeOEMS { get; set; }

        /// <summary>
        /// 仅指定的OEM执行*号查询时包括该列
        /// </summary>
        public string IncludeOEMS { get; set; }


        public bool IsDBField(DBContext context)
        {
            if (Usage == DBFieldUsage.NoField) return false;
            if (context.OEM != null)
            {

                if (ExcludeOEMS != null)
                {
                    if (ExcludeOEMS.ToUpper().IndexOf(context.OEM.Name) > -1)
                    {
                        return false;
                    }
                }
                else if (IncludeOEMS != null)
                {
                    if (IncludeOEMS.ToUpper().IndexOf(context.OEM.Name) == -1)
                    {
                        return false;
                    }
                }

            }
            return true;
        }
    } 
}
